<?php

namespace Telnet\Telnet\Strategy;

use React\Socket\ConnectionInterface;

class CmdConvTree implements CmdInterface {
    public static function handleCmd(array $argc, string $args, ConnectionInterface $connection) {
        if (count($argc) != 1) {
            $connection->write("arg error.\n");
            return;
        }

        $str = '[{"id":200002538,"name":"空心菜类","level":3,"namePath":"蔬菜/豆制品,叶菜类,空心菜类"},{"id":200002537,"name":"香菜类","level":3,"namePath":"蔬菜/豆制品,葱姜蒜椒/调味菜,香菜类"},{"id":200002536,"name":"紫苏/苏子叶","level":3,"namePath":"蔬菜/豆制品,叶菜类,紫苏/苏子叶"},{"id":200002543,"name":"乌塌菜/塌菜/乌菜","level":3,"namePath":"蔬菜/豆制品,叶菜类,乌塌菜/塌菜/乌菜"},{"id":200002542,"name":"菜心/菜苔类","level":3,"namePath":"蔬菜/豆制品,叶菜类,菜心/菜苔类"},{"id":200002540,"name":"马兰头/马兰/红梗菜","level":3,"namePath":"蔬菜/豆制品,叶菜类,马兰头/马兰/红梗菜"},{"id":200002531,"name":"苋菜类","level":3,"namePath":"蔬菜/豆制品,叶菜类,苋菜类"},{"id":200002528,"name":"其他叶菜类","level":3,"namePath":"蔬菜/豆制品,叶菜类,其他叶菜类"}]';
        // var_dump($str);
        $data = json_decode($str, true);
        // var_dump($data);
        // 数据量不大， 可以直接数组遍历， 不用map
        // $dataMap = [];

        $dataArr = [];
        foreach ($data as $da) {
            //分割namePath
            $arr = mb_split(',', $da['namePath']);
            $fristId = self::getPathId($arr[0]);
            $secondId = self::getPathId($arr[1]);


            //是否已添加
            $isAdd = false;

            $item = [
                'id' => $da['id'],
                'id_path' => $fristId . ',' . $secondId . ',' . $da['id'],
                'is_leaf' => 1,
                'level' => 3,
                'name' => $arr[2],
                'name_path' => $da['namePath'],
                'parent_id' => $secondId,
            ];

            foreach ($dataArr as $k => $v) {
                //判断一级目录是否相等
                if($v['id'] != $fristId) {
                    continue;
                }
                
                //遍历二级目录
                foreach ($v['children'] as $sk => $sv) {
                    //二级类目相等
                    if ($sv['id'] == $secondId) {
                        $dataArr[$k]['children'][$sk]['children'][] = $item;
                        $isAdd = true;
                        break;
                    }
                }
                //如果已添加
                if ($isAdd) {
                    break;
                }
                //如果一级目录相等，二级类目都不相等， 需要添加二级类目
                $dataArr[$k]['children'][] = [
                    'id' => $secondId,
                    'id_path' => $fristId . ',' . $secondId,
                    'is_leaf' => 2,
                    'level' => 2,
                    'name' => $arr[1],
                    'name_path' => $arr[0].','. $arr[1],
                    'parent_id' => $fristId,
                    'children' => [$item]
                ];
                $isAdd = true;

            }
            //如果已添加
            if ($isAdd) {
                continue;
            }
            //一级目录也不存在， 添加一二级目录
            $dataArr[] =  ['id' => $fristId,
                'id_path' => $fristId,
                'is_leaf' => 2,
                'level' => 1,
                'name' => $arr[0],
                'name_path' => $arr[0],
                'parent_id' => 0,
                'children' => [[
                    'id' => $secondId,
                    'id_path' => $fristId . ',' . $secondId,
                    'is_leaf' => 2,
                    'level' => 2,
                    'name' => $arr[1],
                    'name_path' => $arr[0] . ',' . $arr[1],
                    'parent_id' => $fristId,
                    'children' => [$item]
                ]]
            ];
        }


        $connection->write(json_encode($dataArr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)."\n");
    }

    public static function getPathId(string $s):string {
        return mb_substr(md5($s), 8, 10);
    }
}